package chapter3.test01;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

import java.sql.SQLException;

import javax.sql.DataSource;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.datasource.SingleConnectionDataSource;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.jta.SpringJtaSynchronizationAdapter;

import vo.UserVO;


public class testDaoMain {
	

	private UserDao userDao;
	private UserVO user1;
	private UserVO user2;
	private UserVO user3;
	
	@Before
	public  void setUp(){
//		UserDao userDao = daoFactory.userDao();
		
//		this.userDao = context.getBean("userDao",UserDao.class);
		 
		this.user1 = new UserVO("junitTest1","1","1");
		 this.user2 = new UserVO("junitTest2","2","2");
		 this.user3 = new UserVO("junitTest3","3","3");
		 
		 
		 DataSource  dataSource  = new SingleConnectionDataSource("jdbc:oracle:thin:@127.0.0.1:1521:xe","ksuniry","73031523",true);
		 
		 this.userDao = new UserDao();
		 userDao.setDataSource(dataSource);
		 
		 
		
		 
	}
	@Test
	public  void addAndGet()throws SQLException{


		
		
		userDao.deleteAll();
		assertThat(userDao.getCount(),is(0));
				
		userDao.add(user1);
		assertThat(userDao.getCount(),is(1));
		assertThat(userDao.get(user1.getId()).getId(),is(user1.getId()));
		
		userDao.add(user2);
		assertThat(userDao.getCount(),is(2));
		assertThat(userDao.get(user2.getId()).getId(),is(user2.getId()));
		
	}  
	@Test(expected=EmptyResultDataAccessException.class)
	public void getUserFailure() throws SQLException{

		userDao.deleteAll();
		assertThat(userDao.getCount(),is(0));
	
		userDao.get("imlpwm_id");
		
		
		
	}
	

}
